import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, Subject, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ToastService } from './toast.service';
import { mockPre } from 'config';
import { Menu } from '../models/menu';
import { ApiResponse } from '../models/api';

@Injectable({
  providedIn: 'root'
})
export class MenuService {

  constructor(
    private $http: HttpClient,
    private toast: ToastService,
  ) { }

  apiPre = `${mockPre}/menu`;

  getMenus(): Observable<ApiResponse<Menu[]>> {
    return this.$http.get<ApiResponse<Menu[]>>(`${this.apiPre}/list`).pipe(
      catchError(err => this.handleError(err, '获取菜单数据失败'))
    );
  }

  private handleError(error: any, title: string, content?: string, param?: any): Observable<any> {
    this.toast.error(title, content, param);
    return throwError(error);
  }
}
